﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace Patchworker
{
	public partial class MainWindow
	{

		private void initToolStripHandlers()
		{
			openToolStripMenuItem.Click += new EventHandler(openToolStripMenuItem_Click);
			exitToolStripMenuItem.Click += new EventHandler(exitToolStripMenuItem_Click);
			aboutToolStripMenuItem.Click += new EventHandler(aboutToolStripMenuItem_Click);
		}

		void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AboutWindow aboutWindow = new AboutWindow();
			aboutWindow.ShowDialog();
		}

		void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		void openToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenFileDialog.ShowDialog();
		}

		private void OpenFileDialog_FileOk(object sender, CancelEventArgs e)
		{
			//파일을 여러개 선택했을 때
			if (OpenFileDialog.FileNames.Length > 1)
			{
				safeFileName = OpenFileDialog.SafeFileNames[0];
				this.fileDirectory = OpenFileDialog.FileNames[0].Substring(0, OpenFileDialog.FileNames[0].Length - this.safeFileName.Length);

				Image[] images = new Image[OpenFileDialog.FileNames.Length];

				for (int i = 0; i < images.Length; i++)
				{
					images[i] = Image.FromFile(OpenFileDialog.FileNames[i]);
				}

				//size check

				Size imageSize = images[0].Size;

				for (int i = 1; i < images.Length; i++)
				{
					if (!imageSize.Equals(images[i].Size))
					{
						//size가 다를 때
						MessageBox.Show("The size of the images are different.");
						return;
					}
				}

				//size가 같을 때 타일링 이미지 생성
				this.TilesGroupBox.Visible = true;
				ConfigContainer.Instance.Mode = ETileMode.Combine;

				ConfigContainer.Instance.Images = images;

				HorNumTileBox.Value = ConfigContainer.Instance.NumHorizontalGrids;

				VerNumTileBox.Enabled = false;

				//list
				TileList.Items.Clear();
				for (int i = 0; i < images.Length; i++)
				{
					ImageListItem newItem = new ImageListItem(images[i], OpenFileDialog.SafeFileNames[i]);
					TileList.Items.Add(newItem);
				}

				SizeTitleLabel.Text = "Atlas Size : ";

				ExportButton.Text = "Export Atlas";

				TileList.KeyDown += new KeyEventHandler(TileList_KeyDown);
			}
			else
			{

				ConfigContainer.Instance.Mode = ETileMode.Split;
				this.TilesGroupBox.Visible = false;


				String fileFullURL = OpenFileDialog.FileName;

				this.FileNameLabel.Text = fileFullURL;

				this.safeFileName = OpenFileDialog.SafeFileName;
				this.fileDirectory = fileFullURL.Substring(0, fileFullURL.Length - this.safeFileName.Length);

				System.Diagnostics.Debug.WriteLine(fileDirectory);


				ConfigContainer.Instance.Image = Image.FromFile(OpenFileDialog.FileName);

				refreshInformations(ConfigContainer.Instance.Atlas);
				calcTileSize(ConfigContainer.Instance.Atlas);

				SizeTitleLabel.Text = "Tiling Size : ";

				ExportButton.Text = "Export Tiles";

				VerNumTileBox.Enabled = true;
			}


			this.AnimationButton.Enabled = true;
			this.ExportButton.Enabled = true;

		}
	}
}
